﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.interfaces;
using System.Xml;
using umbraco;
using log4net;
using System.Reflection;

namespace GrailTechnology.AutoTags.DataType.Installer
{
    public class AddTinyMcePlugin : IPackageAction
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static string file = "/config/tinyMceConfig.config";

        public bool Execute(string packageName, XmlNode xmlData)
        {

            log.Debug("Calling package action for tinyMCE");

            XmlNodeList _newExts = xmlData.SelectNodes("//plugin");

            if (_newExts.Count > 0)
            {
                log.Debug("Found node to insert");

                XmlDocument xdoc = new XmlDocument();
                xdoc.PreserveWhitespace = true;
                xdoc = xmlHelper.OpenAsXmlDocument(file);

                XmlNode xn = xdoc.SelectSingleNode("//tinymceConfig/plugins");

                if (xn != null)
                {
                    log.Debug("Found existing plugins");

                    for (int i = 0; i < _newExts.Count; i++)
                    {
                        XmlNode newExt = _newExts[i];
                        string _alias = newExt.InnerText;

                        log.Debug("new Extension alias : " + _alias);


                        bool insertExt = true;
                        if (xn.HasChildNodes)
                        {
                            foreach (XmlNode ext in xn.ChildNodes)
                            {
                                log.Debug("existing Extension alias : " + ext.InnerText.ToLower());
                                if (ext.InnerText.ToLower().Equals(_alias))
                                    insertExt = false;
                            }
                        }

                        if (insertExt)
                        {
                            xn.AppendChild(xdoc.ImportNode(newExt, true));
                        }
                    }

                    xdoc.Save(System.Web.HttpContext.Current.Server.MapPath(file));
                    return true;
                }
            }
            return false;
        }

        public string Alias()
        {
            return "autotags_addTinyMcePlugin";
        }

        public bool Undo(string packageName, XmlNode xmlData)
        {

            XmlNodeList _newExts = xmlData.SelectNodes("//plugin");

            if (_newExts.Count > 0)
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.PreserveWhitespace = true;
                xdoc = xmlHelper.OpenAsXmlDocument(file);

                XmlNode xn = xdoc.SelectSingleNode("//tinymceConfig/plugins");

                if (xn != null)
                {
                    bool inserted = false;

                    for (int i = 0; i < _newExts.Count; i++)
                    {
                        XmlNode newExt = _newExts[i];
                        string _alias = newExt.InnerText;
                        if (xn.HasChildNodes)
                        {
                            foreach (XmlNode ext in xn.ChildNodes)
                            {
                                if (ext.InnerText.ToLower().Equals(_alias))
                                {
                                    xn.RemoveChild(ext);
                                    inserted = true;
                                }
                            }
                        }
                    }

                    if (inserted)
                    {
                        xdoc.Save(System.Web.HttpContext.Current.Server.MapPath(file));
                        return true;
                    }
                }
            }
            return false;
        }

        #region IPackageAction Members


        public XmlNode SampleXml()
        {
           
            throw new NotImplementedException();
        }

        #endregion
    }
}